/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.Customer;
import org.crystalrange.enterprise.service.CustomerService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class CustomerListController extends BaseController {

    protected CustomerService customerService;
    protected Customer selectedCustomer;
    protected Listbox listboxCustomer;
    protected Search searchCustomer;
    protected List<Customer> customers;

    public CustomerService getCustomerService() {
        return this.customerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public Customer getSelectedCustomer() {
        return this.selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public List<Customer> getCustomers() {
        return this.customers;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchCustomer = new Search(Customer.class);
        this.searchCustomer.setMaxResults(100);
        this.searchCustomer.addFetch("createdBy");
        this.searchCustomer.addFetch("updatedBy");
        this.customers = this.customerService.search(this.searchCustomer);

        component.setAttribute("controller", this);
    }

    public void onCreate$windowCustomerList(Event event) throws Exception {
        this.binder = (AnnotateDataBinder) this.self.getAttribute("binder");
    }

    public void onClick$menuitemCustomerCreate(Event event) throws Exception {
        this.customerCreate();
    }

    public void onClick$menuitemCustomerEdit(Event event) throws Exception {
        this.customerEdit();
    }

    public void onClick$menuitemCustomerDelete(Event event) throws Exception {
        this.customerDelete();
    }

    public void onClick$menuitemCustomerSearch(Event event) throws Exception {
        this.customerSearch();
    }

    public void onClick$menuitemCustomerRefresh(Event event) throws Exception {
        this.customerRefresh();
    }

    public void onClick$toolbarbuttonCustomerCreate(Event event) throws Exception {
        this.customerCreate();
    }

    public void onClick$toolbarbuttonCustomerEdit(Event event) throws Exception {
        this.customerEdit();
    }

    public void onClick$toolbarbuttonCustomerDelete(Event event) throws Exception {
        this.customerDelete();
    }

    public void onClick$toolbarbuttonCustomerSearch(Event event) throws Exception {
        this.customerSearch();
    }

    public void onClick$toolbarbuttonCustomerRefresh(Event event) throws Exception {
        this.customerRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.customerEdit();
    }
    
    private void customerCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/customer.zul");
    }

    private void customerEdit() throws Exception {
        if (this.selectedCustomer != null) {
            this.execution.sendRedirect("~./zul/html/customer.zul?Id=" + this.selectedCustomer.getId().toString());
        }
    }

    private void customerDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxCustomer.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Customer customer = (Customer) selectedItem.getValue();
                    this.customerService.remove(customer);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.customers = this.customerService.search(this.searchCustomer);
        this.binder.loadAll();
    }

    private void customerSearch() throws Exception {
        this.execution.setAttribute("searchCustomer", this.searchCustomer);

        Window window = (Window) this.execution.createComponents("~./zul/html/customerSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchCustomer = (Search) this.execution.getAttribute("searchCustomer");
        }

        this.customers = this.customerService.search(this.searchCustomer);
        this.binder.loadAll();
    }

    private void customerRefresh() throws Exception {
        this.customers = this.customerService.search(this.searchCustomer);
        this.binder.loadAll();
    }
}
